﻿
using Sunny.UI;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;

using WindowsInput;

using System.Drawing;

using System.Text;

using Application = System.Windows.Forms.Application;
using System.Threading;
using System.Collections.Generic;

using System.IO.Ports;



namespace AutoTestSystem
{
    
    public partial class Form1 : Form
    {      
        public static string SelectedKind = string.Empty;
        static string connectStr = string.Empty;
        static string paraPath = string.Empty;
        public static string locPath = string.Empty;
       
      
        public static bool AddStepData = false;
        public static List<StepClass> stepList = new List<StepClass>();
      
        public static double widthRatio = 0;
        public static double heightRatio = 0;


        public SerialPort SerialPort1 = new SerialPort();//串口未使用
        private static string previousData = "";
        public Form1()
        {
            InitializeComponent();

            this.MaximizeBox = false;
            this.Size = new System.Drawing.Size(1080, 768);
            Text = "AutoTestSystem V1.0";
            uiRichTextBox1.ReadOnly = true;
            uiRichTextBox1.Text = string.Empty;
            paraPath = Application.StartupPath + @"\IniParameters.xml";
            locPath = Application.StartupPath + @"\LocData.xml";
            StartToolStripMenuItem.Click += StartToolStripMenuItem_Click;
            SetToolStripMenuItem.Click += SetToolStripMenuItem_Click;
            HelpToolStripMenuItem.Click += HelpToolStripMenuItem_Click;
                   
            Screen screen = Screen.PrimaryScreen;
            widthRatio=(double)pictureBox1.Width / (double)screen.Bounds.Width;
            heightRatio= (double)pictureBox1.Height / (double)screen.Bounds.Height;
          
            ToolClass.IniLocData();         
        }
  
        public void IniXmlData()
        {
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(paraPath);
                XmlNodeList xmlNode = xmlDocument.SelectSingleNode("IniParameters").ChildNodes;

                foreach (XmlNode item in xmlNode)
                {
                    if (item.Name.ToLower() == "connectstring")
                    {
                        connectStr = item.InnerText;
                    }
                    if (item.Name.ToLower()=="comname")
                    {
                        SerialPort1.PortName = item.InnerText;
                    }
                    if (item.Name.ToLower() == "baud")
                    {
                       SerialPort1.BaudRate=Convert.ToInt32(item.InnerText);
                    }
                    if (item.Name.ToLower() == "parity")
                    {
                        if (item.InnerText == "1")
                        {
                            SerialPort1.Parity = Parity.None;
                        }
                        else if (item.InnerText == "2")
                        {
                            SerialPort1.Parity = Parity.Odd;
                        }
                        else if (item.InnerText == "3")
                        {
                            SerialPort1.Parity = Parity.Even;
                        }
                        else if (item.InnerText == "4")
                        {
                            SerialPort1.Parity = Parity.Mark;
                        }
                        else if (item.InnerText == "5")
                        {
                            SerialPort1.Parity= Parity.Space;
                        }   

                    }
                    if (item.Name.ToLower() == "stop")
                    {
                        if (item.InnerText == "1")
                        {
                            SerialPort1.StopBits = StopBits.One;
                        }
                        if (item.InnerText == "2") 
                        {
                            SerialPort1.StopBits = StopBits.Two;  
                        }
                        if (item.InnerText == "3") 
                        {
                            SerialPort1.StopBits = StopBits.OnePointFive;
                        }
                        if (item.InnerText == "4") 
                        {
                            SerialPort1.StopBits = StopBits.None;
                        }
                    }
                    if (item.Name.ToLower() == "num")
                    {
                        SerialPort1.DataBits=Convert.ToInt32(item.InnerText);   
                    }

                }

            }
            catch { MessageBox.Show("数据读取异常"); }
        }

      
      
        private void StartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;


            for (int i = 0; i < stepList.Count; i++)
            {
                if (stepList[i].IsEnable == "✔")
                {
                    stepList[i].StepReachMethod(this);


                    WindowState = FormWindowState.Normal;
                }
            }
          
       
        }

        private void SetToolStripMenuItem_Click(object sender, EventArgs e)
        {

            try
            {
                Edit sf = new Edit();
                sf.ShowDialog();
            }
            catch { }

        }

        private void StopToolStripMenuItem_Click(object sender, EventArgs e)
        {

            StartToolStripMenuItem.Enabled = true;
        }

        private void HelpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String fileName = "D:\\XiaoQiang\\Software\\Dsm\\PQM\\AutoTestSystemOcr - 複製\\AutoTestSystem\\bin\\x86\\Release\\ResultImage\\241120-043839.png";


           uiRichTextBox1.Text= ToolClass.OcrCheckTest(fileName,pictureBox1);
        }

        private  void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            
            //string data = SerialPort1.ReadExisting(); // 读取串口缓冲区中的所有数据
            string newData = SerialPort1.ReadExisting();
            if (newData != previousData)
            {
                WindowState = FormWindowState.Minimized;


                for (int i = 0; i < stepList.Count; i++)
                {
                    if (stepList[i].IsEnable == "✔")
                    {

                        WindowState = FormWindowState.Normal;

                        stepList[i].StepReachMethod(this);

                    }
                }

             
            }

        }


        System.Drawing.Point start, end;//绘制矩形框的起点和终点
        bool blnDraw;//在MouseMove事件中判断是否绘制矩形框
        static bool IsCanSet = false;
        private void pic_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (IsCanSet)
                {
                    start = e.Location;
                    end = e.Location;
                    blnDraw = true;
                }

            }
        }

        private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            this.pictureBox1.Dock = DockStyle.None;
            this.pictureBox1.BorderStyle = BorderStyle.FixedSingle;
            System.Drawing.Size size = this.pictureBox1.Size;
            size.Width += e.Delta;
            if (size.Width > pictureBox1.Image.Width)
            {
                pictureBox1.Width = pictureBox1.Image.Width;
                pictureBox1.Height = pictureBox1.Image.Height;
            }
            else if (size.Width * pictureBox1.Image.Height / pictureBox1.Image.Width < pictureBox1.Parent.Height - 550)
            {
                return;
            }
            else
            {
                pictureBox1.Width = size.Width;
                pictureBox1.Height = size.Width * pictureBox1.Image.Height / pictureBox1.Image.Width;
            }
            pictureBox1.Left = (pictureBox1.Parent.Width - pictureBox1.Width) / 2;
            pictureBox1.Top = (pictureBox1.Parent.Height - pictureBox1.Height) / 2;
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            pictureBox1.Width = this.Width - pictureBox1.Left;//显示控件宽度=窗口宽度-显示控件左外边距
            pictureBox1.Height = this.Height - pictureBox1.Top;//显示控件高度=窗口高度-显示控件左上边距
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripLabel1.Text = SelectedKind+Application.OpenForms.Count;
            


            ToolClass.DisposeMemory();

            int i = 0;
            //try
            //{
            //    if (serialPort2.CDHolding && i == 0)//当串口CD脚为高电平且i=0时执行if语句,RS232 PIN1
            //    {
            //        //Thread.Sleep(100);
            //       ToolClass.Delay(50);
                    
            //        i++;
            //        Application.DoEvents();//释放内存
            //    }
            //    if (serialPort2.CDHolding == false)
            //    {
            //        i = 0;
            //        //TxtEnter.BackColor = Color.White;//將TxtEnter背景顏色變為白色
            //    }
            //}
            //catch
            //{
            //}
        }
      
        
       
      
     
    }

}